package com.ulic.customerManApplication.util.excel;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.ulic.customerManApplication.commonmodule.service.OrganizationService;
import com.ulic.customerManApplication.membermodule.bo.Member;
import com.ulic.customerManApplication.servlet.GetSpringBeanServlet;


public class ExpMembers {
   
	public static void exp(String[] headers,List<Object> objects,String filePath){
		OrganizationService organizationService = (OrganizationService)GetSpringBeanServlet.getBean("organizationService");

		HSSFWorkbook workbook = new HSSFWorkbook();
	    HSSFSheet sheet = workbook.createSheet();
	    workbook.setSheetName(0,"会员信息", (short)1);
	    sheet.setDefaultColumnWidth((short) 15);  
	    HSSFCellStyle style = workbook.createCellStyle();  
	      
	    style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);  
	    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
	    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);  
	    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);  
	    style.setBorderRight(HSSFCellStyle.BORDER_THIN);  
	    style.setBorderTop(HSSFCellStyle.BORDER_THIN);  
	    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  

	    HSSFFont font = workbook.createFont();  
	    font.setColor(HSSFColor.VIOLET.index);  
	    font.setFontHeightInPoints((short) 12);  
	    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
	        
	    style.setFont(font);  
	        
	    HSSFRow row = sheet.createRow(0);  
	    Member member = null;
	    for (short i = 0; i < headers.length; i++)  //设置标题
	        {  
	          HSSFCell cell = row.createCell(i);  
	          cell.setCellStyle(style);  
	          HSSFRichTextString text = new HSSFRichTextString(headers[i]); 
	          cell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          cell.setCellValue(text.toString());  
	         }  
	     int index = 1;   
	     for(Object object: objects){//填写数据
	         member = (Member)object;
	    	 row = sheet.createRow(index++);
	          
	          HSSFCell subComCell = row.createCell((short)0); 
	          subComCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          subComCell.setCellValue(member.getSubComName());
	          
	          HSSFCell subCityCell = row.createCell((short)1); 
	          subCityCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          subCityCell.setCellValue(member.getSubCityName());
	          
	          
	          HSSFCell nameCell = row.createCell((short)2);
	          nameCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          nameCell.setCellValue(member.getMemberName());
	          
	          HSSFCell genderCell = row.createCell((short)3);
	          genderCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          genderCell.setCellValue(member.getGender());
	          
	          HSSFCell idCardCell = row.createCell((short)4); 
	          idCardCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          idCardCell.setCellValue(member.getIdCard());
	          
	          HSSFCell birthCell = row.createCell((short)5); 
	          birthCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          birthCell.setCellValue(member.getBirthday());
	          
	          HSSFCell cellphoneCell = row.createCell((short)6); 
	          cellphoneCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          cellphoneCell.setCellValue(member.getCellPhone());
	          
	          HSSFCell emailCell = row.createCell((short)7); 
	          emailCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          emailCell.setCellValue(member.getEmail());
	          
	          HSSFCell memTypeCell = row.createCell((short)8); 
	          memTypeCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          memTypeCell.setCellValue(member.getMemberType());
	          
	          HSSFCell carBrandCell = row.createCell((short)9); 
	          carBrandCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          carBrandCell.setCellValue(member.getCarBrand());
	          
	          HSSFCell carTypeCell = row.createCell((short)10); 
	          carTypeCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          carTypeCell.setCellValue(member.getCarType());
	          
	          HSSFCell donateNumCell = row.createCell((short)11); 
	          donateNumCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          if(member.getDonateStuNum()==null){
	        	  donateNumCell.setCellValue(0);
	          }else{
	        	  donateNumCell.setCellValue(member.getDonateStuNum());
	          }
	          
	          
	          HSSFCell donateRecordCell = row.createCell((short)12); 
	          donateRecordCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          donateRecordCell.setCellValue(member.getDonateRecord());
	          
	          HSSFCell introCell = row.createCell((short)13); 
	          introCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          introCell.setCellValue(member.getReproduceIntroduce());
	          
	          HSSFCell fatieCell = row.createCell((short)14); 
	          fatieCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          fatieCell.setCellValue(member.getForumPosting());
	          
	          HSSFCell remarkCell = row.createCell((short)15); 
	          remarkCell.setEncoding(HSSFCell.ENCODING_UTF_16);
	          remarkCell.setCellValue(member.getRemark());
	          
	      }
	     OutputStream out  = null;    
		try {
			out= new FileOutputStream(filePath);//读出到服务器上的文件里
			workbook.write(out);
			out.flush();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}

